package com.ruoyi.quartz.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AddWechat;
import com.ruoyi.system.domain.Reports;
import com.ruoyi.system.service.AddWechatService;
import com.ruoyi.system.service.ReportsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Objects;

/*
 *  @author 雨安
 *  类名： ReportsController
 *  创建时间：2025/2/11
 */
@RestController
@RequestMapping("/uni/test")
public class ReportsUniTestController {

    @Resource
    private ReportsService reportsService;

    //查询
    @PostMapping("/get/Reports")
    public AjaxResult getReports(@RequestBody Reports reports){
        //创建分页对象
        Page<Reports> page = new Page<>();
        //设置页码
        page.setCurrent(reports.getCurrent());
        //设置行数
        page.setSize(reports.getSize());
        //执行查询
        reportsService.page(page,new LambdaQueryWrapper<Reports>()
                //举报原因
                .like(StringUtils.isNotBlank(reports.getReportReason()),Reports::getReportReason,reports.getReportReason())
                //举报状态
                .eq(Objects.nonNull(reports.getReportStatus()),Reports::getReportStatus,reports.getReportStatus())
                //逻辑删除
                .eq(Reports::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Reports::getCreateTime) //添加分页语句
                .last(" limit " + (reports.getCurrent() - 1) * reports.getSize() +"," + reports.getSize())

        );
        page.setTotal(reportsService.count(new LambdaQueryWrapper<Reports>()
                //举报原因
                .like(StringUtils.isNotBlank(reports.getReportReason()),Reports::getReportReason,reports.getReportReason())
                //举报状态
                .eq(Objects.nonNull(reports.getReportStatus()),Reports::getReportStatus,reports.getReportStatus())
                //逻辑删除
                .eq(Reports::getDeleteFlag,Boolean.FALSE)
                //排序
                .orderByDesc(Reports::getCreateTime)
        ));
        //返回结果
        return AjaxResult.success(page);
    }

    //删除
    @PostMapping("/delete/Reports/{id}")
    public AjaxResult deleteReports(@PathVariable(value = "id")Long id){
        //调用Service层删除
        reportsService.update(null,new UpdateWrapper<Reports>().lambda()
                //主键id
                .eq(Reports::getReportId,id)
                //设置逻辑删除为 真
                .set(Reports::getDeleteFlag,Boolean.TRUE)
        );
        //返回结果
        return AjaxResult.success("删除成功");
    }
    
    //增加
    @PostMapping("/insert/Reports")
    public AjaxResult insertReports(@RequestBody Reports reports){
        //设置默认值
        reports.setCreateBy(SecurityUtils.getUsername());
        reports.setCreateTime(new Date());
        reports.setUpdateBy(SecurityUtils.getUsername());
        reports.setUpdateTime(new Date());
        reports.setDeleteFlag(Boolean.FALSE);

        //调用增加
        reportsService.save(reports);
        //返回结果
        return AjaxResult.success("新增成功");
    }
    @Resource
    private AddWechatService addWechatService;

    //修改
    @PostMapping("/update/Reports")
    public AjaxResult updateReports(@RequestBody Reports reports){
        //设置默认值
        reports.setUpdateTime(new Date());
        reports.setDeleteFlag(Boolean.FALSE);
        //调用Service层删除
        reportsService.updateById(reports);
        System.out.println(reports);
        System.out.println(1212121212);
        //返回结果
        return AjaxResult.success("修改成功");
    }
}